﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EmBattle : MonoBehaviour {

    public List<int> bufPos;
    private FishView fv;
    List<GameObject> plane = new List<GameObject>();
    List<GameObject> showplane = new List<GameObject>();
    List<GameObject> buffPlane = new List<GameObject>();


    // Use this for initialization
    void Start () {
        fv = GetComponent<FishView>();
        bufPos = new List<int>();
    }

    //按下鼠标，改变地块的颜色
    void OnMouseDown()
    {
        CaluePos();
        GetPlane(showplane);
        foreach (GameObject g in showplane)
        {
            g.GetComponent<Renderer>().material.color = Color.green;
        }
    }
    //松开鼠标，恢复地块的颜色
    void OnMouseUp()
    {
        foreach (GameObject g in showplane)
        {
            g.GetComponent<Renderer>().material.color = Color.white;
        }
        showplane.Clear();
    }

    public void  AddBuffData(int buffID)
    { 

        CaluePos();

        GetPlane(buffPlane);

        //添加魔法
        for (int i = 0; i < buffPlane.Count; i++)
        {
            //print(plane.Count);
            Transform g = null;
            if (buffPlane[i].transform.childCount > 0)
            {
                g = buffPlane[i].transform.GetChild(0);
            }

            if (g != null)
            {
                //g.GetComponent<FishView>().fdata.ManaChange(25);
                BuffData tempbd;
                GameControl._instance.bg.buffDict.TryGetValue(buffID, out tempbd);
                BuffData bd = new BuffData();
                bd.SetMySelf(tempbd);

                g.GetComponent<FishView>().fdata.AddBuff(bd);
            }
        }

        buffPlane.Clear();
    }


    //添加魔法，外部只需要调用一下这个方法即可
    public void AddMana()
    {
        
        CaluePos();

        //获得地块
        GetPlane(plane);

        //添加魔法
        for (int i = 0; i < plane.Count; i++)
        {
            //print(plane.Count);
            Transform g = null;
            if (plane[i].transform.childCount > 0)
            {
                g = plane[i].transform.GetChild(0);
            }

            if (g != null)
            {
                g.GetComponent<FishView>().fdata.ManaChange(25);
            }
        }
        plane.Clear();
    }

    //计算bufPos
    void CaluePos()
    {
        bufPos.Clear();

        //计算坐标
        string temps = fv.fdata.Buff;
        List<int> al = new List<int>();
        string[] s = temps.Split("_"[0]);
        for(int i = 0; i < s.Length; i++)
        {
            al.Add(int.Parse(s[i]));
        }

        //计算出加成的位置，存储起来
        
        for (int i = 0; i < al.Count - 1; i = i + 2)
        {
            int j = i + 1;
            int ip = 0;
            switch (fv.fishPos)
            {
                case 1:
                    if (al[i] > 0 || al[i] < -2) break;
                    if (al[j] > 2 || al[j] < 0) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 2:
                    if (al[i] > 1 || al[i] < -1) break;
                    if (al[j] > 2 || al[j] < 0) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 3:
                    if (al[i] > 2 || al[i] < 0) break;
                    if (al[j] > 2 || al[j] < 0) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 4:
                    if (al[i] > 0 || al[i] < -2) break;
                    if (al[j] > 1 || al[j] < -1) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 5:
                    if (al[i] > 1 || al[i] < -1) break;
                    if (al[j] > 1 || al[j] < -1) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 6:
                    if (al[i] > 2 || al[i] < 0) break;
                    if (al[j] > 1 || al[j] < -1) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 7:
                    if (al[i] > 0 || al[i] < -2) break;
                    if (al[j] > 0 || al[j] < -2) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 8:
                    if (al[i] > 1 || al[i] < -1) break;
                    if (al[j] > 0 || al[j] < -2) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
                case 9:
                    if (al[i] > 2 || al[i] < 0) break;
                    if (al[j] > 0 || al[j] < -2) break;
                    ip = fv.fishPos - al[i] + al[j] * 3;
                    break;
            }
            if (ip != 0) bufPos.Add(ip);
        }
    }

    //获得地块
    void GetPlane(List<GameObject> tempPlane)
    {
        if (gameObject.tag == "Hero")
        {
            for (int i = 0; i < bufPos.Count; i++)
            {
                GameObject go = GameObject.Find("e" + bufPos[i]);
                if (go != null)
                {
                    tempPlane.Add(go);
                }
            }
        }
        else
        {
            for (int i = 0; i < bufPos.Count; i++)
            {
                GameObject go = GameObject.Find("p" + bufPos[i]);
                if (go != null)
                {
                    tempPlane.Add(go);
                }
            }
        }
    }
}
